﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
   public class LoaiPhongDAO
    {
       public List<LoaiPhongDTO> DanhSachLoaiPhong()
       {
           List<LoaiPhongDTO> kq = new List<LoaiPhongDTO>();
           DataProvider dp = new DataProvider();
           string strsql = "select*from LoaiPhong";
           dp.Connect();
           SqlDataReader dr = dp.ExecuteQuery(strsql);
           if (dr.HasRows)
           {
               while (dr.Read())
               {
                   LoaiPhongDTO lp = new LoaiPhongDTO();
                   lp.MaLoaiPhong =int.Parse( dr["MaLoaiPhong"].ToString());
                   lp.SoNguoiToiDa = int.Parse(dr["SoNguoiToiDa"].ToString());
                   lp.TenLoaiPhong = dr["TenLoaiPhong"].ToString();
                   lp.DonGia = float.Parse(dr["DonGia"].ToString());
                   kq.Add(lp);
               }
           }
           dp.Disconnect();
           return kq;
       }
       public LoaiPhongDTO LayLoaiPhongByMaLoaiPhong(int maLoaiPhong)
       {
           LoaiPhongDTO lp = new LoaiPhongDTO();
           DataProvider dp = new DataProvider();
           string strsql = "select*from LoaiPhong where MaLoaiPhong="+maLoaiPhong;
           dp.Connect();
           SqlDataReader dr = dp.ExecuteQuery(strsql);
           dr.Read();
           lp.MaLoaiPhong = int.Parse(dr["MaLoaiPhong"].ToString());
           lp.SoNguoiToiDa = int.Parse(dr["SoNguoiToiDa"].ToString());
           lp.TenLoaiPhong = dr["TenLoaiPhong"].ToString();
           lp.DonGia = float.Parse(dr["DonGia"].ToString());
           dp.Disconnect();
           return lp;
       }
       public Boolean ThemLoaiPhong(LoaiPhongDTO lp)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           string strsql = "insert into LoaiPhong(TenLoaiPhong,DonGia,SoNguoiToiDa)";
           strsql += "values('" + lp.TenLoaiPhong + "'," + lp.DonGia + "," + lp.SoNguoiToiDa + ")";
           dp.Connect();    
           int i = dp.ExecuteNonQuery(strsql);
           dp.Disconnect();
           if (i != 0)
               kq = true;

           return kq;
       }
       public Boolean XoaLoaiPhong(int maLoaiPhong)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           string strsql = "delete from LoaiPhong where MaLoaiPhong="+maLoaiPhong;
           dp.Connect();
           int i = dp.ExecuteNonQuery(strsql);
           dp.Disconnect();
           if (i != 0)
               kq = true;

           return kq;
       }
       public Boolean CapNhatLoaiPhong(LoaiPhongDTO lp)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           string strsql = "update LoaiPhong set TenLoaiPhong='"+lp.TenLoaiPhong+"' and DonGia="+lp.DonGia+" and SoNguoiToiDa="+lp.SoNguoiToiDa;
           strsql += " where MaLoaiPhong=" + lp.MaLoaiPhong;
           dp.Connect();
           int i = dp.ExecuteNonQuery(strsql);
           dp.Disconnect();
           if (i != 0)
               kq = true;

           return kq;
       }

    }
}
